'''
Created on Jul 9, 2011

@author: kafkef
'''       
        
        
from PyQt4.QtGui import *
from PyQt4.QtCore import (QObject, SIGNAL, QAbstractTableModel, Qt, QModelIndex,
                          QVariant)


from dexen.system.client import ui
from dexen.system.client.proxy import JobProxy
from dexen.system.common.base import Action
from dexen.system.client.ui.pages.base_job_page import BaseJobTableModel,\
    BaseJobPage



svr_proxy = ui.svr_proxy

class JobParetoModel(BaseJobTableModel):
    def __init__(self, header_items, pareto_items, pareto_log, parent=None):
        BaseJobTableModel.__init__(self, header_items, pareto_items, 
                                   pareto_log, parent)


class JobParetoPage(BaseJobPage):       
    def __init__(self):
        BaseJobPage.__init__(self)

    # Override base class method
    def on_refresh(self):
        if self._cur_job_id is None: return
        print "JobParetoPage::on_refresh()"
        job_id = self._cur_job_id
        job_proxy = svr_proxy.get_job_cached(job_id)
        assert isinstance(job_proxy, JobProxy)
        pareto_log = job_proxy.get_pareto_log()
        
        # Tokenize the pareto log
        items = pareto_log.split("\n")
        header_items = items[0].split(",")
        pareto_items = []
        for item in items[1:]:
            items = item.split(",")
            # Filter out the possible empty lines at the end of the file
            if len(items) > 1:
                pareto_items.append(items)
        print len(pareto_items), "are received."
        model = JobParetoModel(header_items, pareto_items, pareto_log)
        
        # Save the fresh model
        self._models[job_id] = model
         
        # Update the view based on the refreshed model
        self._table_view.setModel(model)
        self._table_view.resizeColumnsToContents()


